﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.Database;
using Temp.Model;

namespace Temp.Controller
{
    class OntologyController
    {
        private SQLiteFactory sqlf;
        private List<Ontology> ontologyList;
        private SQLite.SQLiteConnection conn;
        private String ontologyTable;//name nung table

        /// <summary>
        /// ung string Object_Ont, Character_Ont, Item_Ont, Story_Ont
        /// </summary>
        /// <param name="ontologyTable"></param>
        public OntologyController() {
            sqlf = new SQLiteFactory();
        }

        public List<Ontology> getOntology(int objID) {
            conn = sqlf.getConnection();
            ontologyList = conn.Query<Ontology>("SELECT * from " + ontologyTable + " WHERE id=" + objID).ToList();
            conn.Close();
            return ontologyList;
        }

        public int getOntId(int objectId1, string type) {
            conn = sqlf.getConnection();
            Ontology o;
            o = conn.Query<Ontology>("SELECT idObj2 FROM " + ontologyTable + " WHERE relation = \"triggers\" AND idObj = ? AND type = ?", objectId1, type).Single();
            int eventId = o.id;
            conn.Close();
            return eventId;
        }

        public List<Ontology> getChildNodes(int parentId, String ontology) {
            List<Ontology> childNodes;
            conn = sqlf.getConnection();
            childNodes = conn.Query<Ontology>("SELECT idObj2 FROM "+ontology+" WHERE relation = \"parent of\" AND idObj = ?", parentId);
            conn.Close();
            return childNodes;
        }

        public List<Ontology> genericQuery(string table, string relation, string concept2) {
            List<Ontology> result;
            conn = sqlf.getConnection();
            System.Diagnostics.Debug.WriteLine("table: " + table);
            System.Diagnostics.Debug.WriteLine("relation: " + relation);
            System.Diagnostics.Debug.WriteLine("concept2 " + concept2);
            result = conn.Query<Ontology>("SELECT * FROM " + table + " WHERE relation = ? COLLATE NOCASE AND concept2 = ? COLLATE NOCASE",relation, concept2);
            conn.Close();
            return result;
        }

        public Ontology getRelationsUse(string table, string concept1)
        {
            conn = sqlf.getConnection();
            
                System.Diagnostics.Debug.WriteLine("table: " + table + " concept1: " + concept1);
                Ontology o = conn.Query<Ontology>("SELECT * FROM " + table + " WHERE concept1 = ? AND relation = ?", concept1, "usedFor").Single();
                conn.Close();
                return o;
            
           
            
        }

        }
}
